package com.example.androidxdemo.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.androidxdemo.App
import com.example.androidxdemo.R
import com.example.androidxdemo.adapter.FunctionAdapter
import com.example.androidxdemo.databinding.FragmentFunctionBinding
import com.example.androidxdemo.viewModel.FunctionListViewModel
import com.example.androidxdemo.viewModel.FunctionViewModel

class FunctionFragment : BaseFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = FragmentFunctionBinding.inflate(inflater, container, false)
        initFunctionsData(binding.rvFunction)
        return binding.root
    }

    private fun initFunctionsData(rv_function: RecyclerView) {
        val functionAdapter = FunctionAdapter()
        rv_function.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        rv_function.adapter = functionAdapter
        functionAdapter.setList(FunctionListViewModel().functionList.value)
        functionAdapter.setOnItemClickListener { _, view, position ->
            when (App.funtions[position].id.value) {
                0 -> {
                    /**
                     * 内存泄漏，具体见下一个fragment
                     */
                    Navigation.findNavController(view).navigate(
                        MainFragmentDirections.actionMainFragmentToMemoryLeakUseCaseFragment(
                            "测试",
                            1,
                            FunctionViewModel("测试功能", 2)
                        )
                    )
                }
                1 -> {
                    /**
                     * navigation管理堆栈，即navigation中action的popupto属性，属性选中的fragment会被放到栈顶，本来在它之上的被弹出。
                     * popUpToInclusive属性:属性选中的fragment也会被弹出
                     */
                    Navigation.findNavController(view).navigate(MainFragmentDirections.actionMainFragmentToExampleCFragment(false))
                }
                2 -> {
                    /**
                     * viewmodel+livedata : LoginViewModel
                     * 根据登录状态的不同，实现条件导航
                     * 包含登录/登出功能
                     */
                    Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_exampleBFragment2)
                }
                3 -> {
                    /**
                     * room
                     */
                    Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_functionDatabaseFragment)
                }
                4 -> {
                    /**
                     * location方法
                     */
                    Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_locationServiceFragment)
                }
                5 -> {
                    /**
                     * 蓝牙通讯
                     */
                    Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_BLEFragment)
                }
            }
        }
    }
}